﻿// 公共
Ext.define('Html5App.util', {
			alternateClassName : 'util',
			statics : {
				// 加载stroe
				storeLoad : function(id) {
					var store = Ext.getStore(id);
					if (store.getCount() < 1) {
						store.load();
					}
				},
				// 加载data数据
				dataLoad : function(view, url, params) {
					Ext.Ajax.request({
								url : url,
								params : params,
								success : function(result, request) {
									result = Ext.decode(result.responseText);
									view.setData(result.result[5]);
								}
							});
				},
				// 加载record数据
				recordLoad : function(record, view, url, params, ckName) {
					if (record.data.ckName) {
						view.setData(record.data);
						return;
					}
					Ext.Ajax.request({
								url : url,
								params : params,
								success : function(result, request) {
									result = Ext.decode(result.responseText);
									record.set(result);
									view.setData(record.data);
								}
							});
				},
				// Viewport添加新项,Viewport之中始终只有一项
				ePush : function(xtype) {
					var me = Ext.Viewport, view = me.getActiveItem();
					if (view && view.getItemId() == xtype) {
						return;
					}
					view = Ext.create(xtype, {
								itemId : xtype
							});
					// 切换
					me.animateActiveItem(view, {
								type : 'slide',
								direction : 'left'
							});
				},
				// 监控Viewport界面切换,切换时销毁旧项
				eActiveitemchange : function() {
					var me = Ext.Viewport;
					me.onAfter('activeitemchange', function(t, value, oldValue,
									eOpts) {
								if (oldValue) {
									// 强制销毁，防止销毁不完全引发错误
									me.remove(oldValue, true);
								}
							});
				},
				/** ************************** 消息提示/加载 ******************************* */
				showMessageMask : function(message, type) {
					var mask = null;
					if (type == 'success') {
						mask = {
							xtype : 'loadmask',
							transparent : true,
							cls : 'custom-message-mask custom-message-mask-success-icon',
							message : message
						};
					} else if (type == 'success-max') {
						mask = {
							xtype : 'loadmask',
							transparent : true,
							cls : 'custom-message-mask custom-message-mask-max custom-message-mask-success-icon',
							message : message
						};
					} else if (type == 'error') {
						mask = {
							xtype : 'loadmask',
							transparent : true,
							cls : 'custom-message-mask custom-message-mask-error-icon',
							message : message
						};
					} else if (type == 'error-max') {
						mask = {
							xtype : 'loadmask',
							transparent : true,
							cls : 'custom-message-mask custom-message-mask-max custom-message-mask-error-icon',
							message : message
						};
					} else if (type == 'warn') {
						mask = {
							xtype : 'loadmask',
							transparent : true,
							cls : 'custom-message-mask custom-message-mask-warn-icon',
							message : message
						};
					} else if (type == 'warn-max') {
						mask = {
							xtype : 'loadmask',
							transparent : true,
							cls : 'custom-message-mask custom-message-mask-max custom-message-mask-warn-icon',
							message : message
						};
					} else if (type == 'info') {
						mask = {
							xtype : 'loadmask',
							transparent : true,
							cls : 'custom-message-mask custom-message-mask-info-icon',
							message : message
						};
					} else if (type == 'info-max') {
						mask = {
							xtype : 'loadmask',
							transparent : true,
							cls : 'custom-message-mask custom-message-mask-max custom-message-mask-info-icon',
							message : message
						};
					}
					Ext.Viewport.mask(mask);
					setTimeout(function() {
						Ext.Viewport.unmask();
					}, 1500);
				},
				hideMessageMask : function() {
					Ext.Viewport.unmask();
				},
				showLoadMask : function(message) {
					var messageStr = "";
					if (message) {
						messageStr = message;
					} else {
						messageStr = "正在處理...";
					}
					Ext.Viewport.mask({
						xtype : 'loadmask',
						cls : 'custom-load-mask',
						message : messageStr
					});
				},
				hideLoadMask : function() {
					Ext.Viewport.unmask();
				},
				showLoadMaskMax : function(message) {
					var messageStr = "";
					if (message) {
						messageStr = message;
					} else {
						messageStr = "正在處理...";
					}
					Ext.Viewport.mask({
						xtype : 'loadmask',
						cls : 'custom-load-mask custom-load-mask-max',
						message : messageStr
					});
				},
				hideLoadMaskMax : function() {
					Ext.Viewport.unmask();
				},
				// 验证
				valid : function(model, from) {
					var tmpModel = Ext.create(model), me = this, errors, valid;
					from.updateRecord(tmpModel);
					errors = tmpModel.validate();
					valid = errors.isValid();
					if (!valid) {
						errors.each(function(err) {
									me.showMessage(err.getMessage(), true, false);
									return;
								});
					}
					return valid;
				},
				// 重写ajax
				overrideAjax : function() {
					var me = this;
					// 开始加载
					Ext.Ajax.on('beforerequest', function(connection, options) {
								if (!options.hidMessage) {
									me.showMessage('正在努力加載中...', true, false);
								}
							});
					// 加载成功
					Ext.Ajax.on('requestcomplete',
							function(connection, options) {
								me.hideMessage();
							});
					// 加载失败
					Ext.Ajax.on('requestexception', function(connection,
									options) {
								if (!options.hidMessage) {
									me.showMessage('加載失敗，請稍後再試...', true, false);
								}
							});
				},
				initializeDatabase : function() {
					//打开或创建数据库
					config.database = openDatabase("WebSQL_FW", "1.0", "手机数据库", 8 * 1024 * 1024);
					config.database.transaction(
				        function (tx) {
				        	tx.executeSql(
				            	"create table if not exists LastSelectCustomer (" +
				            		"CustomerCode TEXT" +
				            	")",[],function (tx, result) {},function (tx, error) {}
				            );
				        	tx.executeSql(
				        			"create table if not exists LastSelectArea (" +
				        			"CodeTerr TEXT," +
				        			"CodeNameTerr TEXT" +
				        			")",[],function (tx, result) {},function (tx, error) {}
				        	);
				        	tx.executeSql(
				            	"create table if not exists LastLoginSalesMan (" +
				            		"SalesCode TEXT," +
				            		"Passwd TEXT," +
				            		"SalesName TEXT" +
				            	")",[],function (tx, result) {},function (tx, error) {}
				            );
				        	tx.executeSql(
				            	"create table if not exists SalesMan (" +
				            		"SalesCode TEXT," +
				            		"Passwd TEXT," +
				            		"SalesName TEXT" +
				            	")",[],function (tx, result) {},function (tx, error) {}
				            );
				        	tx.executeSql(
				            	"create table if not exists Customer (" +
				            		"CustomerCode TEXT," +
				            		"SalesCode TEXT," +
				            		"CustomerName TEXT," +
				            		"DeliveryAddress TEXT," +
				            		"Payment TEXT," +
				            		"Address1 TEXT," +
				            		"Address2 TEXT," +
				            		"Address3 TEXT," +
				            		"Phone TEXT," +
				            		"Fax TEXT," +
				            		"CodeTerr TEXT," +
				            		"ChannelID TEXT," +
				            		"NameCtac TEXT," +
				            		"CodeTerm TEXT," +
				            		"TextDesc TEXT," +
				            		"AMTCRLIMT TEXT," +
				            		"AllInvAmt TEXT," +
				            		"Balance TEXT," +
				            		"T1 TEXT," +
				            		"A1 TEXT," +
				            		"Note TEXT" +
				            	")",[],function (tx, result) {},function (tx, error) {}
				            );
				        	tx.executeSql(
				            	"create table if not exists Area (" +
				            		"SalesCode TEXT," +
				            		"CodeTerr TEXT," +
				            		"NameTerr TEXT" +
				            	")",[],function (tx, result) {},function (tx, error) {}
				            );
				            tx.executeSql(
				            	"create table if not exists TAllSalesDataPK (" +
				            		"ORDUNIQ TEXT," +
				            		"LINENUM TEXT," +
				            		"ORDNUMBER TEXT," +
				            		"CUSTOMER TEXT," +
				            		"BILNAME TEXT," +
				            		"ORDDATE TEXT," +
				            		"SALESPER1 TEXT," +
				            		"ITEM TEXT," +
				            		"DESC TEXT," +
				            		"ACCTSET TEXT," +
				            		"CNTLACCT TEXT," +
				            		"OPTFLD4 TEXT," +
				            		"OPTFLD5 TEXT," +
				            		"ORIGQTY TEXT," +
				            		"QTYSHIPPED TEXT," +
				            		"ORDUNIT TEXT," +
				            		"UNITPRICE TEXT," +
				            		"QTYSHPTODT TEXT," +
				            		"QTYBACKORD TEXT," +
				            		"QTYORDERED TEXT," +
				            		"SRCode TEXT," +
				            		"OCTN TEXT," +
				            		"OPC TEXT," +
				            		"SCTN TEXT," +
				            		"SPC TEXT," +
				            		"OrderAmt TEXT," +
				            		"ShipAmt TEXT," +
				            		"OrderSU TEXT," +
				            		"ShipSU TEXT," +
				            		"PICKSEQ TEXT," +
				            		"DistrictCode TEXT," +
				            		"DistrictName TEXT," +
				            		"OrdType TEXT," +
				            		"Category TEXT," +
				            		"ShipTo TEXT," +
				            		"ShipAddress TEXT" +
				            	")",[],function (tx, result) {},function (tx, error) {}
				            );
				            tx.executeSql(
				            	"create table if not exists CustomerBrandTarget (" +
				            		"CustomerCode TEXT," +
				            		"BrandCode TEXT," +
				            		"SalesCode TEXT," +
				            		"Target TEXT," +
				            		"Actual TEXT," +
				            		"IndexStr TEXT" +
				            	")",[],function (tx, result) {},function (tx, error) {}
				            );
				            tx.executeSql(
				            	"create table if not exists Brand (" +
				            		"BrandCode TEXT," +
				            		"SalesCode TEXT," +
				            		"BrandName TEXT," +
				            		"CategoryCode TEXT" +
				            	")",[],function (tx, result) {},function (tx, error) {}
				            );
				            tx.executeSql(
				            	"create table if not exists Item (" +
				            		"ItemCode TEXT," +
				            		"SalesCode TEXT," +
				            		"ItemName TEXT," +
				            		"BrandID TEXT," +
				            		"Price TEXT," +
				            		"OPTFLD4 TEXT," +
				            		"optfld7 TEXT," +
				            		"optfld8 TEXT," +
				            		"optfld9 TEXT," +
				            		"optfld10 TEXT," +
				            		"optfld11 TEXT," +
				            		"Unit TEXT," +
				            		"optfld3 TEXT," +
				            		"OPTAMT TEXT," +
				            		"OPTFLD5 TEXT," +
				            		"UNITWGT TEXT" +
				            	")",[],function (tx, result) {},function (tx, error) {}
				            );
				            tx.executeSql(
				            	"create table if not exists CustomerAddress (" +
				            		"IDCUST TEXT," +
				            		"IDCUSTSHPT TEXT," +
				            		"SalesCode TEXT," +
				            		"NAMELOCN TEXT," +
				            		"TEXTSTRE1 TEXT," +
				            		"TEXTSTRE2 TEXT," +
				            		"TEXTSTRE3 TEXT," +
				            		"TEXTSTRE4 TEXT" +
				            	")",[],function (tx, result) {},function (tx, error) {}
				            );
				            tx.executeSql(
				            	"create table if not exists ItemShort (" +
				            		"ItemCode TEXT," +
				            		"ItemName TEXT," +
				            		"Brand TEXT," +
				            		"Category TEXT," +
				            		"ItemShort TEXT," +
				            		"UpdateTime TEXT" +
				            	")",[],function (tx, result) {},function (tx, error) {}
				            );
				            tx.executeSql(
				            	"create table if not exists ItemChinese (" +
				            		"ItemCode TEXT," +
				            		"ItemName TEXT," +
				            		"PICKINGSEQ TEXT" +
				            	")",[],function (tx, result) {},function (tx, error) {}
				            );
				            tx.executeSql(
				            	"create table if not exists SalesAllotment (" +
				            		"SALESCODE TEXT," +
				            		"ITEMCODE TEXT," +
				            		"DATE TEXT," +
				            		"RESETDATE TEXT," +
				            		"DESCRIPTION TEXT," +
				            		"UNIT TEXT," +
				            		"QUANTITY TEXT" +
				            	")",[],function (tx, result) {},function (tx, error) {}
				            );
				            tx.executeSql(
				            	"create table if not exists SalesBrandTarget (" +
				            		"BrandCode TEXT," +
				            		"SalesCode TEXT," +
				            		"Target TEXT," +
				            		"Actual TEXT," +
				            		"IndexStr TEXT" +
				            	")",[],function (tx, result) {},function (tx, error) {}
				            );
				            //OrderHead
				            tx.executeSql(
				            	"create table if not exists OrderHead (" +
				            		"OrderNo number," +
				            		"SalesCode TEXT," +
				            		"OrderDate TEXT," +
				            		"DeliveryDate TEXT," +
				            		"CustomerCode TEXT," +
				            		"CustomerName TEXT," +
				            		"Address number," +
				            		"AddressStr TEXT," +
				            		"Payment number," +
				            		"TotalAmount TEXT," +
				            		"NetAmount TEXT," +
				            		"TotalQty TEXT," +
				            		"DetailCount number," +
				            		"CreateTime TEXT," +
				            		"UpdateTime TEXT," +
				            		"ConvertFlag number," +
				            		"confirmDate TEXT," +
				            		"isSend number" +
				            	")",[],function (tx, result) {},function (tx, error) {}
				            );
				            //OrderDetail
				            tx.executeSql(
				            	"create table if not exists OrderDetail (" +
				            		"OrderNo number," +
				            		"DetailNo number," +
				            		"SalesCode TEXT," +
				            		"ItemCode TEXT," +
				            		"ItemName TEXT," +
				            		"Unit TEXT," +
				            		"ICOQty TEXT," +
				            		"StockQty TEXT," +
				            		"OrderQty TEXT," +
				            		"Price TEXT," +
				            		"SubTotal TEXT," +
				            		"confirmDate TEXT," +
				            		"isSend number" +
				            	")",[],function (tx, result) {},function (tx, error) {}
				            );
				            var maxOrderNo = 1;
				            Ext.maxOrderNo = maxOrderNo;
				        	tx.executeSql("select max(OrderNo) as maxOrderNo from OrderHead", [],
				                function (tx, result) {
				    				if(result.rows.item(0)['maxOrderNo'] != null){
				    					maxOrderNo = result.rows.item(0)['maxOrderNo'];
				    					maxOrderNo = Number(Number(maxOrderNo)+1);
				    				}
				                	Ext.maxOrderNo = maxOrderNo;
				                },
				                function (tx, error){
				                	
				                }
				            );
				            //FOrderHead
				            tx.executeSql(
				            	"create table if not exists FOrderHead (" +
				            		"OrderNo TEXT," +
				            		"SalesCode TEXT," +
				            		"OrderDate TEXT," +
				            		"DeliveryDate TEXT," +
				            		"CustomerCode TEXT," +
				            		"Address TEXT," +
				            		"Payment number," +
				            		"TotalAmount TEXT," +
				            		"NetAmount TEXT," +
				            		"TotalQty TEXT," +
				            		"DetailCount TEXT," +
				            		"CreateTime TEXT," +
				            		"UpdateTime TEXT," +
				            		"ConvertFlag TEXT," +
				            		"OrgOrderNo TEXT," +
				            		"FaxTag TEXT," +
				            		"BatchNo TEXT" +
				            	")",[],function (tx, result) {},function (tx, error) {}
				            );
				            //FOrderDetail
				            tx.executeSql(
				            	"create table if not exists FOrderDetail (" +
				            		"OrderNo TEXT," +
				            		"DetailNo TEXT," +
				            		"SalesCode TEXT," +
				            		"ItemCode TEXT," +
				            		"ICOQty TEXT," +
				            		"StockQty TEXT," +
				            		"OrderQty TEXT," +
				            		"Price TEXT," +
				            		"SubTotal TEXT," +
				            		"OrgOrderNo TEXT" +
				            	")",[],function (tx, result) {},function (tx, error) {}
				            );
				            //Golden45Sales
				            tx.executeSql(
				            	"create table if not exists Golden45Sales (" +
				            		"ntype number," +
				            		"AreaCode TEXT," +
				            		"SalesCode TEXT," +
				            		"AreaName TEXT," +
				            		"CustomerCode TEXT," +
				            		"CustomerName TEXT," +
				            		"IsGolden45 TEXT," +
				            		"Golden45s number," +
				            		"Orders number," +
				            		"Other1 TEXT," +
				            		"Other2 TEXT," +
				            		"Other3 TEXT" +
				            	")",[],function (tx, result) {},function (tx, error) {}
				            );
				            //LocalFile
				            tx.executeSql(
				            	"create table if not exists LocalFile (" +
				            		"filetype TEXT," +
				            		"filename TEXT," +
				            		"filepath TEXT" +
				            	")",[],function (tx, result) {},function (tx, error) {}
				            );
				        }
				    );
				},
				// app初始化执行
				inIt : function() {
					this.eActiveitemchange();
					this.overrideAjax();
					this.initializeDatabase();
				}
			}
		});